//
<template>
  <div class="update-gender">
    <van-picker
      show-toolbar
      title="选择性别"
      :columns="columns"
      :default-index="value"
      @cancel="$emit('close')"
      @confirm="onConfirm"
      @change="onPickerChange"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user.js'
export default {
  name: 'UpdateGender',
  components: {},
  props: {
    value: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      columns: ['男', '女'],
      localGender: this.value, // 当前性别
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 确定事件
    async onConfirm(value, index) {
      //   增加loading交换
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0,
      })
      //   console.log(index)
      try {
        //   1.调用接口
        await updateUserProfile({
          gender: index,
        })
        // 2.关闭弹层
        this.$emit('close')
        // 3.修改父组件的gender的值
        this.$emit('input', index)
        // 4.提示信息
        this.$toast.success('更新成功')
      } catch (err) {
        this.$toast.fail('更新失败')
      }
    },
    // 选择器发生变化的时候
    onPickerChange(picker, value, index) {
      this.localGender = index
    },
  },
}
</script>

<style scoped lang="less"></style>
